package com.linux.huhx.generate;

import com.linux.huhx.baseBean.ClassJavaBean;
import com.linux.huhx.baseBean.FieldJavaBean;
import com.linux.huhx.baseBean.PojoJavaBean;
import com.linux.huhx.task.ConsoleTask;
import com.linux.huhx.task.FileTask;
import com.linux.huhx.utils.CodeRegrexUtils;
import com.linux.huhx.utils.CodeUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by huhx on 2017-06-15.
 */
public class GenerateCode {
    // 这个可以设计成责任链的形式
    public void consoleGenerate() {
        ConsoleTask consoleTask = new ConsoleTask();
        consoleTask.doTaskExecute();
    }

    public void fileGenerate() {
        FileTask fileTask = new FileTask();
        fileTask.doTaskExecute();
    }

    public static void main(String[] args) {
        GenerateCode generateCode = new GenerateCode();
        generateCode.consoleGenerate();
//        generateCode.fileGenerate();
    }

    @Test
    public void generateBean_test() {
        // package
        String packageName = "com.csii.mca.datamodel.qrcode";

        // import info
        List<String> importList = new ArrayList<>();
//        String import1 = "com.csii.mca.datamodel.qrcode.MCQRCodeAddiReqRequest;";
//        String import2 = "com.csii.mca.datamodel.qrcode.MCQRCodeAddiReqResponse;";
//        importList.add(import1);
//        importList.add(import2);

        // class info
        ClassJavaBean classInfoMap = new ClassJavaBean();
        classInfoMap.setClassName("MCQRCodeAddiReqRequest");
        classInfoMap.setExtendsInfo("BaseRequest");
        classInfoMap.setImplementInfo("BaseInterface");

        // field info
        List<FieldJavaBean> fieldBeanList = new ArrayList<>();
        FieldJavaBean fieldJavaBean1 = new FieldJavaBean();
        fieldJavaBean1.setFieldType("Date");
        fieldJavaBean1.setFieldName("issCode");
        fieldJavaBean1.setFieldComment("发C8:D29");
        FieldJavaBean fieldJavaBean2 = new FieldJavaBean();
//        fieldJavaBean2.setFieldType("String");
        fieldJavaBean2.setFieldName("hello");
        fieldJavaBean2.setFieldComment("发C8:D29");
        fieldBeanList.add(fieldJavaBean1);
        fieldBeanList.add(fieldJavaBean2);

        // 总体的设置
        PojoJavaBean pojoJavaBean = new PojoJavaBean();
        pojoJavaBean.setPackageName(packageName);
        pojoJavaBean.setImportLists(importList);
        pojoJavaBean.setClassJavaBean(classInfoMap);
        pojoJavaBean.setFieldBeanList(fieldBeanList);

        // 根据PojoJavaBean的信息产生java文件
        gernerateCodeByPojoBean(pojoJavaBean);
    }

    private void gernerateCodeByPojoBean(PojoJavaBean pojoJavaBean) {
        // package info
        String packageInfo = "package " + pojoJavaBean.getPackageName();

        // import info
        List<String> importList = pojoJavaBean.getImportLists();
        StringBuffer importBuffer = new StringBuffer();
        for (String string : importList) {
            importBuffer.append("import " + string + "\n");
        }

        // class info
        ClassJavaBean classJavaBean = pojoJavaBean.getClassJavaBean();
        String className = classJavaBean.getClassName();
        String extendsName = classJavaBean.getExtendsInfo();
        String implementName = classJavaBean.getImplementInfo();
        StringBuffer classInfo = new StringBuffer("public class " + className);
        if (StringUtils.isNotEmpty(extendsName)) {
            classInfo.append(" extends " + extendsName);
        }
        if (StringUtils.isNotEmpty(implementName)) {
            classInfo.append(" implements " + implementName);
        }
        classInfo.append(" {");

        // field info
        String fieldInfo = "\tprivate {fieldType} {fieldName}; // {fieldComment}\n";
        String getsetInfo = "\tpublic String get{upperName}() {\n" +
                "\t\treturn {fieldName};\n" +
                "\t}\n" +
                "\n" +
                "\tpublic void set{upperName}(String {fieldName}) {\n" +
                "\t\tthis.{fieldName} = {fieldName};\n" +
                "\t}\n";
        StringBuffer filedBuffer = new StringBuffer();
        List<FieldJavaBean> fieldJavaBeans = pojoJavaBean.getFieldBeanList();
        // private String username;
        for (FieldJavaBean fieldJavaBean : fieldJavaBeans) {
            filedBuffer.append(CodeRegrexUtils.baseReplaceFromJavaBean(fieldInfo, fieldJavaBean));
        }
        filedBuffer.append("\n");
        // getUsername、setUsername method
        for (FieldJavaBean fieldJavaBean : fieldJavaBeans) {
            filedBuffer.append(CodeRegrexUtils.baseReplaceFromJavaBean(getsetInfo, fieldJavaBean));
        }
        filedBuffer.append("}");

        // total info
        StringBuffer totalBuffer = new StringBuffer();
        totalBuffer.append(packageInfo + "\n\n");
        if (importList.size() > 0) {
            totalBuffer.append(importBuffer + "\n");
        }
        totalBuffer.append(classInfo + "\n");
        totalBuffer.append(filedBuffer);
        System.out.println(totalBuffer.toString());

        // 生成文件
        File file = new File("D:/huhx", className + ".java");
        try {
            FileUtils.write(file, totalBuffer.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
